package com.tianci.appstore.ui.loading;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.utils.SkyConstants;

public class LoadingView extends LinearLayout {
    private Context mContext;
    private ImageView image;
    private Animation loadingAnim;
    private TextView loadingText;

    public LoadingView(Context context) {
        super(context);
        this.mContext = context;
        this.setOrientation(LinearLayout.VERTICAL);
        this.setGravity(Gravity.CENTER);
        // TODO Auto-generated constructor stub
        image = new ImageView(context);
        SkinManager.setBackground(image, R.drawable.loading);
        this.addView(image, new LayoutParams(SkyConstants.Div(92), SkyConstants.Div(92)));
        loadingAnim = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF,
                0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        loadingAnim.setInterpolator(new LinearInterpolator());
        loadingAnim.setDuration(1500);
        loadingAnim.setRepeatCount(-1);
    }

    public void startLoading() {
        this.setVisibility(View.VISIBLE);
        image.startAnimation(loadingAnim);
    }

    public void stopLoading() {
        loadingAnim.cancel();
        this.setVisibility(View.INVISIBLE);
    }

    public void setLoadingText(String str) {
        if (str == null || str.trim().length() == 0) {
            if (loadingText != null)
                loadingText.setVisibility(View.GONE);
            return;
        }
        if (loadingText == null) {
//            loadingText = UICommon.newTextView(mContext, UICommon.tipTextSize, R.color.tipColor);
            loadingText = new TextView(mContext);
            loadingText.setTextSize(SkyConstants.Dpi(36));
//            loadingText.setSingleLine();
            loadingText.setLineSpacing(mContext.getResources().getDimension(R.dimen.block_spacing), 1.0f);
            loadingText.setTextColor(SkinManager.getColor(R.color.tipColor));//mContext.getResources().getColor(R.color.tipColor));
            int textPadding = SkyConstants.Div(20);
            loadingText.setPadding(textPadding, textPadding, textPadding, textPadding);
            loadingText.setGravity(Gravity.CENTER);
            loadingText.setVisibility(View.GONE);
            this.addView(loadingText, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        }
        loadingText.setText(str);
        loadingText.setVisibility(View.VISIBLE);
    }
}
